//
//  CycleViewLineCreat.m
//  MyLLPay
//
//  Created by ddy on 2019/3/4.
//  Copyright © 2019年 cycle. All rights reserved.
//

#import "CycleViewLineCreat.h"
#import "Masonry.h"
#define kLineCreatFont(f) [UIFont systemFontOfSize:f]
#define kLineCreatTextColor [UIColor colorWithRed:151/255.0 green:151/255.0 blue:151/255.0 alpha:1.0]
#define kLineCreatRGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define kLineCreatLeftMargin 0 //commonView距离父控件的距离
#define kLineCreatRowHeight 50 //行高
#define kLineCreatBackGroundColor [UIColor whiteColor]

#define kLineCreatChildControlLeftMargin 15//左边/右边文字距离父控件距离 ViewLineTypeThirdLine
#define kLineCreatChildControlLeftCommonMargin 10//ViewLineTypeCommon
#define kLineCreatIconWH 25

static NSInteger commonTextFieldTag = 11;

@interface CycleViewLineCreat()
{
    // 1.图标 文字 输入框 type = ViewLineTypeCommon
    UIImageView *_commonIconImageView;
    UILabel *_commonTitleLabel;
    UITextField *_commonTextField;
    
    // 2.标题:内容  标题:内容  标题:内容
    UILabel *_firstLabel;
    UILabel *_secondLabel;
    UILabel *_thirdLabel;
    
}

/** 传入类型( ViewLineTypeThreeLine) */
@property (nonatomic, assign) ViewLineType lineType;

/** 传入类型
 ///图标 文字 输入框 (一个imageView 一个label 一个UITextfield)
 ViewLineCommonTypeAll = 0,
 ///文字 输入框 (一个label 一个UITextfield)
 ViewLineCommonTypeNoIcon = 1,
 ///图标 输入框 (一个imageView 一个UITextfield)
 ViewLineCommonTypeNoTitle = 2,
 */
@property (nonatomic, assign) ViewLineCommonType commonType;

@end

@implementation CycleViewLineCreat


/**
 * 1.初始化
 * @param detailType 该类型下有三种情况:1.图标 标题 文本框 2.图标 文本框 3.标题 文本框
 */
- (instancetype)initWithLineCommonType:(ViewLineCommonType)detailType frame:(CGRect)frame {
    if (self == [super initWithFrame:frame]) {
        self.frame = frame;
        self.commonType = detailType;
            ////1.图标 文字 输入框
            UIView *commonView = [self cycle_lineTypeCommonViewWithCommonType:detailType];
            [self addSubview:commonView];
            [commonView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.mas_equalTo(0);
                make.left.mas_equalTo(kLineCreatLeftMargin);
                make.right.mas_equalTo(-kLineCreatLeftMargin);
            }];
    }
    return self;
}

/**
 * 2.初始化
 * @param type : 传入类型
 */
- (instancetype)initWithLineType:(ViewLineType)type frame:(CGRect)frame  {
    if (self = [super initWithFrame:frame]) {
        self.frame = frame;
        self.lineType = type;
        
         if (self.lineType == ViewLineTypeThreeLine) {
                ////1.标题:内容  标题:内容  标题:内容
                UIView *ThreeLineView = [self cycle_lineTypeThreeLineView];
                [self addSubview:ThreeLineView];
                [ThreeLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.bottom.mas_equalTo(0);
                    make.left.mas_equalTo(kLineCreatLeftMargin);
                    make.right.mas_equalTo(-kLineCreatLeftMargin);
                }];
            }
    }
    return self;
}

#pragma mark --- target
- (void)c_textFieldEditingChange:(UITextField *)textField {
    if (commonTextFieldTag == textField.tag) {
        !self.textBlock ?: self.textBlock(textField);
    }
}

#pragma mark --- 1.图标 文字 输入框
- (UIView *)cycle_lineTypeCommonViewWithCommonType:(ViewLineCommonType)type {
    UIView *mainView = [UIView new];
    mainView.backgroundColor = kLineCreatBackGroundColor;
    //图标
    UIImageView *iconImageView = [UIImageView new];
    _commonIconImageView = iconImageView;
//    if(type != ViewLineCommonTypeNoIcon){
        [mainView addSubview:iconImageView];
//    }
    
    //标题
    UILabel *titleLabel = [UILabel new];
    _commonTitleLabel = titleLabel;
    titleLabel.text = @"";
    titleLabel.font = kLineCreatFont(15.f);
    titleLabel.textColor = kLineCreatRGB(51,51, 51);
    titleLabel.textAlignment = NSTextAlignmentLeft;
//    if(type != ViewLineCommonTypeNoTitle){
        [mainView addSubview:titleLabel];
//    }
    
    //输入框
    UITextField *textField = [UITextField new];
 
    textField.tag = commonTextFieldTag;
    _commonTextField = textField;
    textField.textAlignment = NSTextAlignmentLeft;
    [mainView addSubview:textField];
    textField.borderStyle = UITextBorderStyleNone;
    textField.placeholder  = @"";
    textField.font         = kLineCreatFont(14);
    textField.textColor    = kLineCreatRGB(51,51, 51);
    //输入框 文字编辑改变
    [textField addTarget:self action:@selector(c_textFieldEditingChange:) forControlEvents:UIControlEventEditingChanged];
    
    ///布局
    void(^AllBlock)(void) = ^(){
        titleLabel.hidden = NO;
        iconImageView.hidden = NO;
        
        ///宽度自适应
        [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
//        titleLabel.preferredMaxLayoutWidth = width;//给一个maxWidth
//        [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//设置
//        titleLabel.numberOfLines = 0;//设置换行
        
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kLineCreatChildControlLeftCommonMargin);
            make.centerY.mas_equalTo(mainView.mas_centerY);
            make.width.height.mas_equalTo(kLineCreatIconWH);
        }];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(iconImageView.mas_right).offset(kLineCreatChildControlLeftCommonMargin);
            make.centerY.mas_equalTo(iconImageView.mas_centerY);
            //        make.width.priorityLow();
            
        }];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).offset(kLineCreatChildControlLeftCommonMargin);
            make.top.bottom.mas_equalTo(mainView);
            make.right.mas_equalTo(-kLineCreatChildControlLeftCommonMargin);
        }];
    };
    
    void(^NoIconBlock)(void) = ^(){
        titleLabel.hidden = NO;
        iconImageView.hidden = YES;
        
        ///宽度自适应
        [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        //        titleLabel.preferredMaxLayoutWidth = width;//给一个maxWidth
        //        [titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];//设置
        //        titleLabel.numberOfLines = 0;//设置换行
        [titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kLineCreatChildControlLeftCommonMargin);
            make.centerY.mas_equalTo(mainView.mas_centerY);
        }];
        [textField mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(titleLabel.mas_right).offset(kLineCreatChildControlLeftCommonMargin);
            make.top.bottom.mas_equalTo(mainView);
            make.right.mas_equalTo(-kLineCreatChildControlLeftCommonMargin);
        }];
    };
    
    void(^NoTitleBlock)(void) = ^(){
        titleLabel.hidden = YES;
        iconImageView.hidden = NO;
        
        [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(kLineCreatChildControlLeftCommonMargin);
            make.centerY.mas_equalTo(mainView.mas_centerY);
            make.width.height.mas_equalTo(kLineCreatIconWH);
        }];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(iconImageView.mas_right).offset(kLineCreatChildControlLeftCommonMargin);
            make.top.bottom.mas_equalTo(mainView);
            make.right.mas_equalTo(-kLineCreatChildControlLeftCommonMargin);
        }];
    };
    
    //布局
    switch (type) {
        case ViewLineCommonTypeAll:
            AllBlock();
            break;
        case ViewLineCommonTypeNoIcon:
            NoIconBlock();
            break;
        case ViewLineCommonTypeNoTitle:
            NoTitleBlock();
            break;
            
        default:
            break;
            
            
    }
    
    
    
    return mainView;
}
/**
 * 初始化内容 包含图片,标题,文本输入框
 */
- (void)cycle_setCommonViewWithIcon:(NSString *)imageName title:(NSString *)titleString fieldPlaceHolder:(NSString *)placeHolderString {
    _commonIconImageView.image = [UIImage imageNamed:imageName];
    _commonTitleLabel.text = titleString;
    _commonTextField.placeholder = placeHolderString;
}
/**
 * 设置 标题 占位文字
 */
- (void)cycle_setCommonViewWithTitle:(NSString *)titleString fieldPlaceHolder:(NSString *)placeHolderString {
    _commonTitleLabel.text = titleString;
    _commonTextField.placeholder = placeHolderString;
}

/**
 * 设置 图片名称 占位文字
 */
- (void)cycle_setCommonViewWithIcon:(NSString *)imageName fieldPlaceHolder:(NSString *)placeHolderString {
    _commonIconImageView.image = [UIImage imageNamed:imageName];
    _commonTextField.placeholder = placeHolderString;
}
///设置图标宽度高度
- (void)cycle_setCommonViewWithIconWidth:(CGFloat)width height:(CGFloat)height {
    [_commonIconImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height);
    }];
}
///设置标题属性
- (void)cycle_setCommonViewWithTitleFontSize:(CGFloat)fontSize TitleColor:(UIColor *)color {
    _commonTitleLabel.font = kLineCreatFont(fontSize);
    _commonTitleLabel.textColor = color;
}
///设置文本属性
- (void)cycle_setCommonViewWithTextFieldFontSize:(CGFloat)fontSize titleColor:(UIColor *)color {
    _commonTextField.font = kLineCreatFont(fontSize);
    _commonTextField.textColor = color;
}
///设置文本框占位文字颜色
- (void)cycle_setCommonViewWithPlaceHolderColor:(UIColor *)placeHolderColor {
    [_commonTextField setValue:placeHolderColor forKeyPath:@"_placeholderLabel.textColor"];
}
///设置输入框边框type
- (void)cycle_setCommonViewWithBorderStyleType:(UITextBorderStyle)style {
    _commonTextField.borderStyle = style;
}
///设置输入框keyBoardType
- (void)cycle_setCommonViewWithKeyBoardType:(UIKeyboardType)type {
    _commonTextField.keyboardType = type;
}
///设置输入框keyBoardType,边框样式UITextBorderStyle
- (void)cycle_setCommonViewWithKeyBoardType:(UIKeyboardType)type andBorderStyle:(UITextBorderStyle)style {
    _commonTextField.keyboardType = type;
    _commonTextField.borderStyle = style;
}



#pragma mark --- 2.标题:内容  标题:内容  标题:内容
- (UIView *)cycle_lineTypeThreeLineView {
    UIView *commonView = [UIView new];

    commonView.backgroundColor = kLineCreatBackGroundColor;
    //第一条数据Label
    UILabel *firstLabel = [UILabel new];
    firstLabel.text     = @"--";
    firstLabel.font     = kLineCreatFont(12.f);
    firstLabel.textColor     = kLineCreatTextColor;
    firstLabel.textAlignment = NSTextAlignmentLeft;
    _firstLabel              = firstLabel;
    [commonView addSubview:firstLabel];

    //第二条数据Label
    UILabel *secondLabel = [UILabel new];
    secondLabel.text     = @"--";
    secondLabel.font     = kLineCreatFont(12.f);
    secondLabel.textColor = kLineCreatTextColor;
    secondLabel.textAlignment = NSTextAlignmentCenter;
    _secondLabel = secondLabel;
    [commonView addSubview:secondLabel];
    //第三条数据Label
    UILabel *thirdLabel = [UILabel new];
    thirdLabel.text     = @"--";
    thirdLabel.font     = kLineCreatFont(12.f);
    thirdLabel.textColor = kLineCreatTextColor;
    thirdLabel.textAlignment = NSTextAlignmentRight;
    _thirdLabel = thirdLabel;
    [commonView addSubview:thirdLabel];
    
    
    //布局
    [firstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kLineCreatChildControlLeftMargin);
        make.top.bottom.mas_equalTo(commonView);
        make.height.mas_equalTo(kLineCreatRowHeight);
    }];
    [secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(firstLabel);
        make.left.mas_equalTo(firstLabel.mas_right);
        make.width.height.mas_equalTo(firstLabel);
    }];
    [thirdLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(firstLabel);
        make.left.mas_equalTo(secondLabel.mas_right);
        make.right.mas_equalTo(commonView.mas_right).offset(-kLineCreatChildControlLeftMargin);
        make.width.height.mas_equalTo(firstLabel);
    }];
    
    
    
    
    
    return commonView;
}
/**
 * 1.富文本数据
 */
- (void)cycle_setFirstAttributedContent:(NSAttributedString *)content1 secondContent:(NSAttributedString *)content2 thirdContent:(NSAttributedString *)content3 {
    _firstLabel.attributedText = content1;
    _secondLabel.attributedText= content2;
    _thirdLabel.attributedText = content3;
}
/**
 * 2.普通数据
 */
- (void)cycle_setFirstContent:(NSString *)content1 secondContent:(NSString *)content2 thirdContent:(NSString *)content3 {
    _firstLabel.text = content1;
    _secondLabel.text= content2;
    _thirdLabel.text = content3;
}

/**
 * 3.设置字体大小和颜色(按序传入)
 */
- (void)cycle_setLabelFontWithArray:(NSArray <UIFont *>*)fontArray andColorArray:(NSArray <UIColor *>*)colorArray {
    
    if (fontArray.count == 3) {

        UIFont *font1 = [fontArray[0] isKindOfClass:[UIFont class]] ? fontArray[0] : kLineCreatFont(12.f);
        UIFont *font2 = [fontArray[1] isKindOfClass:[UIFont class]] ? fontArray[1] : kLineCreatFont(12.f);
        UIFont *font3 = [fontArray[2] isKindOfClass:[UIFont class]] ? fontArray[2] : kLineCreatFont(12.f);
        
        _firstLabel.font  = font1;
        _secondLabel.font = font2;
        _thirdLabel.font  = font3;
    }
    if (colorArray.count == 3) {
        UIColor *color1 = [colorArray[0] isKindOfClass:[UIColor class]] ? colorArray[0] : kLineCreatTextColor;
        UIColor *color2 = [colorArray[1] isKindOfClass:[UIColor class]] ? colorArray[1] : kLineCreatTextColor;
        UIColor *color3 = [colorArray[2] isKindOfClass:[UIColor class]] ? colorArray[2] : kLineCreatTextColor;
        
        _firstLabel.textColor  = color1;
        _secondLabel.textColor = color2;
        _thirdLabel.textColor  = color3;
    }
    
}
@end
